package com.wgts.wgts.mvp.view.activity.goodsinfo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.labels.LabelData;
import com.wgts.wgts.control.labels.LabelsView;
import com.wgts.wgts.localbean.ProductSpecifications;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsTypeLabsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private int NUMBER = 0x0001;

    private int LABS = 0x0002;

    private Context context;

    private List<ProductSpecifications> specifications;

    private LabsSelectListener labsSelectListener;

    private MountListener mountListener;

    private GoodsNumberViewHolder goodsNumberViewHolder;

    public GoodsTypeLabsAdapter(Context context, List<ProductSpecifications> specifications) {
        this.context = context;
        this.specifications = specifications;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == NUMBER) {
            return new GoodsNumberViewHolder(LayoutInflater.from(context).inflate(R.layout.special_item_number, viewGroup, false));
        }
        return new GoodsTypeViewHolder(LayoutInflater.from(context).inflate(R.layout.specifica_item_labs, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        if (viewHolder instanceof GoodsTypeViewHolder) {
            ArrayList<LabelData> labelDatas = new ArrayList<>();

            ((GoodsTypeViewHolder) viewHolder).tvItemName.setText(specifications.get(i).attributeName);


            for (String labItem : specifications.get(i).attributeValueList) {
                LabelData labelData = new LabelData();
                labelData.setName(labItem);
                labelData.setId(i);
                labelDatas.add(labelData);
            }

            ((GoodsTypeViewHolder) viewHolder).labsItem.setLabels(labelDatas);

            //((GoodsTypeViewHolder) viewHolder).labsItem.setSelects(0);

            ((GoodsTypeViewHolder) viewHolder).labsItem.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
                @Override
                public void onLabelSelectChange(View label, LabelData data, boolean isSelect, int position) {
                    if (labsSelectListener != null) {
                        labsSelectListener.onLabsSelect(data, i, isSelect);
                    }
                    if (isSelect) {
                        ((TextView) label).setTextColor(ContextCompat.getColor(context, R.color.whrite));
                        //screenId = data.getId();
                    } else {
                        ((TextView) label).setTextColor(ContextCompat.getColor(context, R.color.black));
                        //screenId = typeId;
                    }

                }
            });


        } else if (viewHolder instanceof GoodsNumberViewHolder) {
            goodsNumberViewHolder = (GoodsNumberViewHolder) viewHolder;
            ((GoodsNumberViewHolder) viewHolder).linearAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mountListener != null) {
                        mountListener.addMount();
                    }
                }
            });

            ((GoodsNumberViewHolder) viewHolder).linearRaduice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mountListener != null) {
                        mountListener.produceMount();
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return specifications.size() + 1;
    }

    public class GoodsTypeViewHolder extends RecyclerView.ViewHolder {
        public TextView tvItemName;

        public LabelsView labsItem;

        public GoodsTypeViewHolder(@NonNull View itemView) {
            super(itemView);
            labsItem = itemView.findViewById(R.id.labs_item);
            tvItemName = itemView.findViewById(R.id.tv_item_name);
        }
    }

    public class GoodsNumberViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.linear_raduice)
        LinearLayout linearRaduice;
        @BindView(R.id.shopping_car_change_number)
        TextView shoppingCarChangeNumber;
        @BindView(R.id.linear_add)
        LinearLayout linearAdd;
        @BindView(R.id.shopping_number_ly)
        LinearLayout shoppingNumberLy;
        public GoodsNumberViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == specifications.size()) {
            return NUMBER;
        }
        return LABS;
    }

    public interface LabsSelectListener {
        void onLabsSelect(LabelData data, int position, boolean isSelect);
    }

    public void setLabsSelectListener(LabsSelectListener labsSelectListener) {
        this.labsSelectListener = labsSelectListener;
    }

    public interface MountListener {
        void addMount();

        void produceMount();
    }

    public void setMountListener(MountListener mountListener) {
        this.mountListener = mountListener;
    }

    public void notifyMount(int mount) {
        if (goodsNumberViewHolder != null) {
            goodsNumberViewHolder.shoppingCarChangeNumber.setText(String.valueOf(mount));
        }
    }
}
